{% extends "base.html" %}
{% block head %}
    <link href="/css/unblog.css" rel="stylesheet">
{% endblock %}
{% block sidebar %}
<li class="nav-header"><span onclick = "switch_topic_preview();">Последние темы</span></li>
<li>
    {% if add_topic_form %}
        <a onclick="document.getElementById('add_topic_form').style.display='block';">Хочу новую тему</a>
        <form action="" method="POST" id='add_topic_form' style="display: none">{% csrf_token %}
            {{ add_topic_form.topic_title }}
            <a onclick="this.parentNode.submit();">Добавить</a>
        </form>
    {% endif %}
    {% include "topic_list_snippet.html" %}
</li>
<li class="nav-header"><a href="{% url unblog_topic_list %}">Все темы</a></li>
{% endblock %}

{% block content %}
    {% if topic %}
        {% if edit_topic_form %}
            <div class="item_edit">
                <a onclick="document.getElementById('edit_topic_form').style.display='block';" class="al_right">Редактировать тему</a>
            </div>
            <form action="" method="POST" id='edit_topic_form' style="display: none">{% csrf_token %}
                <input type="hidden" name="edit_topic" value="1">
                {{ edit_topic_form.topic_title }}
                <a onclick="this.parentNode.submit();" class="al_right">Сохранить</a>
            </form>
            <div class="item_delete">{% with "YES!" as delete_current %}{% include 'delete_token.html' %}{% endwith %}</div>
        {% endif %}
        <h1>{{topic}}</h1>

        {% if profile %}<div class='item_info'> {{ topic.access_comment }} </div>{% endif %}
    {% else %}
        <h1>Все подряд</h1>
        <div class='item_info'>Вам надо залогиниться и выбрать тему, чтобы что-то в нее написать.</div>
    {% endif %}

    {% if allowed %}
    <form action="" method="POST" name="add_container">{% csrf_token %}
        {{ add_form.text }}
        <br><input type="submit" value="Отправить">
    </form>
    {% endif %}


    <div class="posts_container">
        <div class="pagination">
            <span class="step-links">
                {% if pages.has_previous %}
    <a class="al_left" href="{% if topic %}{% url unblog_topic topic.id %}{% else %}{% url unblog %}{% endif %}?p={{ pages.previous_page_number }}">&larr; сюда</a>
                {% endif %}

                <span class="current">
                    Страница {{ pages.number }} из {{ pages.paginator.num_pages }}.
                </span>
                {% if pages.has_next %}
    <a class="al_right" href="{% if topic %}{% url unblog_topic topic.id %}{% else %}{% url unblog %}{% endif %}?p={{ pages.next_page_number }}">туда &rarr;</a>
                {% endif %}
            </span>
        </div>

        {% for one in unblogs %}
            <div class='unblog_post'>
                {% if one.user.userprofile.show_avatar %}
                    {% if one.user.userprofile.photo %}<img src="{{one.user.userprofile.photo}}" class='avatar_medium al_left' >
                    {% else %}<div class='avatar_medium al_left empty'></div>
                    {%endif%}
                {% endif %}
                <div class='unblog_info'>
                    <div class='item_info al_left'>
                        <a href="{{one.get_permalink }}">
                            {{ one.user }} {{one.datetime|date:"H:i d.m.Y"}}
                        </a>
                    </div>
                    {% if not topic %}
                        <div class='item_info'> 
                            (<a href="{% url unblog_topic one.topic.id %}">{{one.topic}}</a>)
                        </div> 
                    {% endif %} 
                </div>
                <div class='unblog_text' name='unblog_text' id="unblog_{{one.pk}}">
                    {{one.show|safe|removetags:"div"|linebreaksbr}}
                </div>
                <div class='unblog_textcopy' name='unblog_textcopy' id="unblog_copy_{{one.pk}}">
                    {{one.text|safe|removetags:"div"|linebreaksbr}}
                </div>

                {% if one.corrected %}<div class='item_info al_left'>Редактировано {{one.corrected|date:"H:i d.m.Y"}}</div>{% endif %}
                <form action="" method="POST" name="edit_container" id="edit_container_{{one.pk}}">
                </form>
                <div name="control_container" id="control_container_{{one.pk}}">
                    {% if one.user == user or user.is_staff %}
                        <div class='item_edit'><a onclick = 'set_edit_form({{one.pk}}); return false;'>Редактировать</a></div>
                        <div class='item_delete'>{% include 'delete_token.html' %}</div>
                    {% endif %}
                </div>

            </div>
        {% endfor %}
    </div>

    <script type='text/javascript'>
    function set_edit_form(post_id) {
        var empty_form = "{% csrf_token %}";
        empty_form += '<textarea id="id_blog_edit_area" rows=10 cols=65 name="blog_edit_area" /></textarea>';
        empty_form += '<input id="edit_id" name="edit_id" type="hidden" value="'+post_id+'">';
        empty_form += '<a class="item_save" onclick="this.parentNode.submit(); return false;">Сохранить</a>';
        empty_form += '<a class="item_save" onclick="reset_controls(); return false;">Я передумал!</a>';

        var text = document.getElementById('unblog_copy_'+post_id).innerHTML;
        text = text.replace(/^[\s]+/g, "").replace(/[\s]+$/g, ""); //Удаляем пробелы в начале и в конце
        text = text.replace(/<br>/g, ""); //Удаляем теги переноса строки, они тут должны быть нативные
        text = text.replace(/&amp;/g, "&");
        text = text.replace(/&quot;/g, '"');
        text = text.replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&amp;/g, "&");
        //alert(forms.length);

        document.getElementById('edit_container_'+post_id).innerHTML = empty_form;
        document.getElementById('id_blog_edit_area').value = text;
        document.getElementById('unblog_'+post_id).style.display = 'none';
        document.getElementById('control_container_'+post_id).style.display = 'none';
    }
    function reset_controls() {
        forms = document.getElementsByName('edit_container');
        for (i in forms) { if (forms[i].innerHTML) forms[i].innerHTML = ''; };
        texts = document.getElementsByName('unblog_text');
        for (i in texts) { if (texts[i].style) texts[i].style.display = 'block'; };
        controls = document.getElementsByName('control_container');
        for (i in controls) { if (controls[i].style) controls[i].style.display = 'block'; };
    }

    </script>

{% endblock %}       
